package cn.itcast.controller;

import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import cn.itcast.service.OrderService;
import cn.itheima.orders.dto.OrdersPageQueryDTO;
import cn.itheima.orders.pojo.Orders;
import cn.itheima.orders.vo.OrderOverViewVO;
import cn.itheima.orders.vo.OrderStatisticsVO;
import cn.itheima.orders.vo.OrderVO;
import cn.itheima.sys.vo.BusinessDataVO;
import cn.itheima.sys.vo.SalesTop10ReportVO;
import cn.itheima.sys.vo.TurnoverReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "管理端-订单接口")
@AllArgsConstructor
public class OrderController {

    private OrderService orderService;

    @GetMapping("/getSumByDate")
    public List<TurnoverReportVO> getSumByDate(LocalDate begin, LocalDate end) {

        return orderService.getSumByDate(begin, end);
    }

    @GetMapping("/countByMap")
    public Integer countByMap(Map<String, Object> map) {
        return orderService.countByMap(map);
    }

    @GetMapping("/getBusinessData")
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        return orderService.getBusinessData(begin, end);
    }

    @GetMapping("/getBusinessListData")
    public List<BusinessDataVO> getBusinessListData(LocalDateTime begin, LocalDateTime end){
        return orderService.getBusinessListData(begin, end);
    }

    @GetMapping("/countDishAndSetmealByTime")
    public List<SalesTop10ReportVO> countDishAndSetmealByTime(LocalDate begin, LocalDate end){
        return orderService.countDishAndSetmealByTime(begin, end);
    }

    @GetMapping("/getAllCountByStatus")
    OrderOverViewVO getAllCountByStatus(LocalDateTime start, LocalDateTime end){
        return orderService.getAllCountByStatus(start,end);
    }

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO 搜索条件
     * @return 无
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageBean> page(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单分页查询:{}", ordersPageQueryDTO);
        PageBean pageBean = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 取消订单
     *
     * @param orders 要取消的订单信息
     * @return 无
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancel(@RequestBody Orders orders) {
        log.info("取消订单：{}", orders.getId());
        orderService.cancel(orders);
        return Result.success();
    }

    /**
     * 完成订单
     *
     * @param id 要完成的订单id
     * @return 无
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> complete(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @param orders 要拒单的订单信息
     * @return 无
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result<String> rejection(@RequestBody Orders orders) {
        log.info("拒单：{}", orders.getId());
        orderService.rejection(orders);
        return Result.success();
    }

    /**
     * 接单
     *
     * @param orders 要接的订单信息
     * @return 无
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<String> confirm(@RequestBody Orders orders) {
        log.info("接单：{}", orders.getId());
        orderService.confirm(orders);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id 要派送的订单id
     * @return 无
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable Long id) {
        log.info("派送订单：{}", id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 查询订单详情
     *
     * @param id 要查询的订单id
     * @return 订单的详情
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> getDetail(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        OrderVO orderVO = orderService.getDetailById(id);
        return Result.success(orderVO);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return 订单的详情
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("各个状态的订单数量统计");
        OrderStatisticsVO orderStatisticsVO = orderService.getStatistics();
        return Result.success(orderStatisticsVO);
    }
}
